class AddGenreJoins < ActiveRecord::Migration
  def self.up
    change_table :genres do |t|
      t.remove :genre_id
    end

    create_table :genre_parent, :id => false do |t|
      t.integer :parent_id, :null => false
      t.integer :child_id, :null => false
    end

    create_table :artists_genres do |t|
      t.integer :artist_id, :null => false
      t.integer :genre_id, :null => false
      t.integer :rank, :default => 1
    end

    create_table :editions_genres do |t|
      t.integer :edition_id, :null => false
      t.integer :genre_id, :null => false
      t.integer :rank, :default => 1
    end

    create_table :genres_songs do |t|
      t.integer :genre_id, :null => false
      t.integer :song_id, :null => false
      t.integer :rank, :default => 1
    end

    #Foreign key definations
    add_foreign_key(:genres, :forums, :column => :forum_id)
    add_foreign_key(:genre_parent, :genres, :column => :parent_id)
    add_foreign_key(:genre_parent, :genres, :column => :child_id)
    add_foreign_key(:editions_genres, :editions, :column => :edition_id)
    add_foreign_key(:editions_genres, :genres, :column => :genre_id)
    add_foreign_key(:genres_songs, :songs, :column => :song_id)
    add_foreign_key(:genres_songs, :genres, :column => :genre_id)
    add_foreign_key(:artists_genres, :artists, :column => :artist_id)
    add_foreign_key(:artists_genres, :genres, :column => :genre_id)
  end

  def self.down
    change_table :genres do |t|
      t.integer :genre_id
    end
   
    remove_foreign_key(:genres, :column => forum_id)

    drop_table :genre_parent
    drop_table :artists_genres
    drop_table :editions_genres
    drop_table :genres_songs
  end
end

